package major;

import org.apache.kafka.clients.consumer.Consumer;
import org.apache.kafka.clients.consumer.ConsumerRecord;
import org.apache.kafka.clients.consumer.ConsumerRecords;

import java.io.FileNotFoundException;
import java.io.RandomAccessFile;
import java.time.Duration;
import java.util.Arrays;

//观察小张　topic purple
public class ObserverMum {
    public static void main(String[] args) throws Exception {

        Consumer<String, String> consumer = new ConsumerBuilder(
                "mum", Arrays.asList("xiaozhang")).build();
        Integer mumAge;

        RandomAccessFile target = new RandomAccessFile(
                "/home/xieweig/document/kafkatest.txt", "rw");

        while (true){
            ConsumerRecords<String, String> records = consumer.poll(Duration.ofSeconds(1));
            for (ConsumerRecord<String, String> record : records) {
                Integer age;
                try{
                    age=Integer.parseInt(record.value());
                }catch (Exception e){
                    age=18;
                }
                mumAge=age+30;
                String k = String.format("小张今年%d岁，妈妈或爸爸今年%d岁", age,mumAge);
                System.out.println(k);
                System.out.println("tell ??? "+target.getFilePointer()+"...length"+target.length());

                target.write(String.format("小张今年%d岁,此时父母%d岁\n", age,mumAge).getBytes());
            }
        }
    }
}
